package com.xianggu.designMode.chainMethod.demo02;

import java.util.function.Supplier;

/**
 * @author chengjy
 * @date 2025/3/15
 */
public abstract class Handler {

    public static final Integer ONE = 1;
    public static final Integer THREE = 3;
    public static final Integer SEVEN = 7;
    private Supplier<Boolean> supplier = () -> true;
    private Integer start;
    private Integer end;

    private String job;

    // 下一个处理者
    private Handler next;

    public final boolean submit(LeaveRequest leaveRequest){
        //判断是否是自己的处理范围
        if(isInCharge( leaveRequest.getLeaveDay())){
            audit(leaveRequest);
            System.out.println("===============================");
            Boolean value = supplier.get();
            if (value) {
                System.out.println(getJob() + "审批通过！");
            } else {
                System.out.println(getJob() + "审批不通过！");
            }
            return value;
        }else if(next != null && leaveRequest.getLeaveDay() > end){
            System.out.println(getJob() +"审批通过，交给"+ next.getJob() +"审批");
            System.out.println("===============================");
            return next.submit(leaveRequest);
        }else{
            System.out.println("流程结束！");
        }
        return false;
    }

    public abstract void audit(LeaveRequest leaveRequest);
    public Handler(Integer start, Integer end) {
        this.start = start;
        this.end = end;
    }

    public final Handler setNext(Handler next){
        this.next = next;
        return next;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getJob() {
        return job;
    }

    private boolean isInCharge(Integer leaveDay){
        return leaveDay >= start && leaveDay <= end;
    }

    public Handler setSupplier(Supplier<Boolean> supplier){
        this.supplier = supplier;
        return this;
    };
}
